/* 
* wsdlpull- A C++ parser  for WSDL  (Web services description language)
* Copyright (C) 2005-2007 Vivek Krishna
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
*/

#ifdef _WIN32
#include <windows.h>
#include <winreg.h>
#include <wininet.h>
//#include <w3c.h>
#pragma comment(lib, "wininet.lib")
#endif

#ifdef HAVE_CONFIG_H //
#include <config.h>
#endif

#ifndef _WIN32
#include <termios.h>
#include <unistd.h>
#include <errno.h>
#ifdef WITH_CURL
#include <curl/curl.h>
#endif

#endif

#include <time.h>
#include <fstream>
#include <map>
#include "XmlUtils.h"

const std::string ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

std::map<std::string,std::string> urlCache_;
//put all I/O and std::string manip  utiliy functions here

int
XmlUtils::parseInt (std::string s, int radix)
{
  int len = s.size ();
  int value = 0;
  if (s.empty ())
    return -1;
  for (int i = 0; i < len; i++) {
    if (radix == 10) {
      if (s[i] <= '9' && s[i] >= 0)
	value = (i == 0) ? (s[i] - '0') : radix * value + (s[i] - '0');

      else
	throw std::exception ();
    }
    else if (radix == 16) {
      //assumes that the encoding format has arranges the alphabets and numbers in increasing order
      if (s[i] <= '9' && s[i] >= 0)
	value = (i == 0) ? (s[i] - '0') : radix * value + (s[i] - '0');

      else if (s[i] <= 'F' && s[i] >= 'A')
	value =
	  (i ==
	   0) ? (s[i] - 'A') + 10 : radix * value + (s[i] - 'A') + 10;

      else if (s[i] <= 'f' && s[i] >= 'a')
	value =(i ==0) ? (s[i] - 'a') + 10 : radix * value + (s[i] - 'a') + 10;
    }
  }
  return value;
}


std::ostream &
XmlUtils::dbsp (std::ostream & str)
{
  return str << "  ";
}


std::ostream &
XmlUtils::blk (std::ostream & str)
{
  return str << std::endl << "*************" << std::endl;
}


std::string
XmlUtils::acceptSecretKey(const std::string& field)
{
  std::cerr<<field<<": ";
  char password [50];
#ifndef _WIN32  
  tcflag_t oflags;
  struct termios term;
  tcgetattr(STDIN_FILENO, &term);
  oflags = term.c_lflag;
  term.c_lflag = oflags & ~(ECHO | ECHOK | ICANON);
  term.c_cc[VTIME] = 1;
  tcsetattr(STDIN_FILENO, TCSANOW, &term);

  scanf("%s", password);

  term.c_lflag = oflags;
  term.c_cc[VTIME] = 0;
  tcsetattr(STDIN_FILENO, TCSANOW, &term);
#else
  scanf("%s", password);
#endif
  return password;
}


static bool g_bProxy = false;
static std::string g_sProxyHost; //host:port format
static std::string g_sProxyUser;
static std::string g_sProxyPass;

bool

XmlUtils::getProxy ()
{
  return g_bProxy;
}

void

XmlUtils::setProxy (const bool bProxy)
{
  g_bProxy = bProxy;
}

std::string

XmlUtils::getProxyHost ()
{
  return g_sProxyHost;
}

void

XmlUtils::setProxyHost (const std::string& sProxyHost)
{
  g_sProxyHost = sProxyHost;
}

std::string

XmlUtils::getProxyUser ()
{
  return g_sProxyUser;
}

void

XmlUtils::setProxyUser (const std::string& sProxyUser)
{
  g_sProxyUser = sProxyUser;
}

std::string

XmlUtils::getProxyPass ()
{
  return g_sProxyPass;
}

void

XmlUtils::setProxyPass (const std::string& sProxyPass)
{
  g_sProxyPass = sProxyPass;
}
